package com.briup.cms.aop;

import com.briup.cms.bean.BaseLog;
import com.briup.cms.constant.CmsConstants;
import com.briup.cms.dao.BaseLogMapper;
import com.briup.cms.utils.JudgeAdmin;
import com.briup.cms.utils.UserInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;


@Aspect    //切面类
@Component  //ioc
public class LogAspect {
    @Autowired
    private BaseLogMapper mapper;

    // 1.切入点的规则
    @Pointcut("execution(* com.briup.cms.web.controller.*.*(..)) && @annotation(Logging)")
    public void logCutPoint(){
    }

    @Before("logCutPoint()")//前置通知
    public void BeforeAdvice(){
        Map<String, Object> userInfo = UserInfo.getUserInfo();
        BaseLog baseLog = new BaseLog();
        baseLog.setLogUsername((String) userInfo.get("username"));
        baseLog.setLogRealname((String) userInfo.get("realname"));
        baseLog.setLogRequestMethod((String) userInfo.get("method"));
        baseLog.setLogRequestUri((String) userInfo.get("uri"));
        baseLog.setLogTime(new Date());
        //判断是否是管理员操作，，管理员操作不用生成记录
        //判断法一，通过baseLog对象获取用户名与admin进行比较
//        String admin="admin";
//        if(!baseLog.getLogUsername().equals(admin)) mapper.insert(baseLog);
        //判断法二，通过登录方法中获取数据库中的的role值修改工具类中的静态方法的boolean类型判断是不是管理员
//        System.out.println(JudgeAdmin.Admin);
        if(JudgeAdmin.Admin) mapper.insert(baseLog);
        //判断法三，通过将数据库中的role值加入到token值中，通过baseLog对象获取role值进行判断
//        System.out.println(userInfo.get("role"));
//        if((int)userInfo.get("role")!=0)mapper.insert(baseLog);
    }

}
